package com.example.a1725185505.works.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public abstract class BaseFragment extends Fragment implements View.OnClickListener{
    public View view=null;
    public Context context;

    public abstract @LayoutRes
    int getLayoutFile();

    abstract public void initView(View view);
    abstract public void initEvent();
    abstract public void initData();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    @Nullable
    @Override
    //绑定xml
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutFile(), container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initEvent();
        initData();
    }

    /**
     * Toast的简化
     */
    public void showToast(String text){
        Toast.makeText(context,text, Toast.LENGTH_SHORT).show();
    }

    /**
     * Log
     */
    public void showLog(String text){
        Log.d(getClass().getName(),text);
    }

    /**
     * 简化view.findViewById
     */
    public <T extends View> T findViewById(@IdRes int id) {
        return view.findViewById(id);
    }

}
